package com.CmJava;


import com.CmJava.entity.MyInfo;
import com.CmJava.mapper.MyInfoMapper;
import com.CmJava.session.SqlSession;
import com.CmJava.sqlSessionFactoryBuilder.SqlSessionFactoryBuilder;
import com.CmJava.sqlSessionFactoryBuilder.sqlSessionFactory.SqlSessionFactory;
import com.CmJava.sqlSessionFactoryBuilder.xml.Resources;


import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession session = sqlSessionFactory.openSession();

        //mybatis只有在执行sql时候才会连接数据库,所以手写框架也是在这里面先连接数据库
        //后执行sql

        //(1)直接使用session的方法进行执行，传参需要传mapper.xml中sql的id（namespace+id）
        MyInfo myInfo = session.selectOne("com.CmJava.mapper.MyInfoMapper.getMyInfo",1);

        //(2)session获取mapper的代理对象，让代理对象执行，这也是日常开发中是用mybatis后台的实际操作流程
        MyInfoMapper mapper = session.getMapper(MyInfoMapper.class);
        MyInfo myInfo1 = mapper.getMyInfo(1);

        System.out.println(myInfo);
        System.out.println(myInfo1);

    }
}
